home *** CD-ROM | disk | FTP | other *** search
- /* ANTLRTokenBuffer.C
- *
- * SOFTWARE RIGHTS
- *
- * We reserve no LEGAL rights to the Purdue Compiler Construction Tool
- * Set (PCCTS) -- PCCTS is in the public domain. An individual or
- * company may do whatever they wish with source code distributed with
- * PCCTS or the code generated by PCCTS, including the incorporation of
- * PCCTS, or its output, into commerical software.
- *
- * We encourage users to develop software with PCCTS. However, we do ask
- * that credit is given to us for developing PCCTS. By "credit",
- * we mean that if you incorporate our source code into one of your
- * programs (commercial product, research project, or otherwise) that you
- * acknowledge this fact somewhere in the documentation, research report,
- * etc... If you like PCCTS and have developed a nice tool with the
- * output, please mention that you developed it using PCCTS. In
- * addition, we ask that this header remain intact in our source code.
- * As long as these guidelines are kept, we expect to continue enhancing
- * this system and expect to make other tools available as they are
- * completed.
- *
- * ANTLR 1.31
- * Terence Parr
- * Parr Research Corporation
- * with Purdue University and AHPCRC, University of Minnesota
- * 1989-1995
- */
-
- typedef int TokenType; // fool AToken.h into compiling
- #include "config.h"
- #include ATOKENBUFFER_H
-
- ANTLRTokenBuffer::
- ANTLRTokenBuffer(ANTLRTokenStream *input, int k, int cs)
- {
- this->input = input;
- this->k = k;
- buffer_size = chunk_size = cs;
- #ifdef OLD
- buffer = (ANTLRAbstractToken **)calloc(chunk_size,
- sizeof(ANTLRAbstractToken *));
- #else
- buffer = ((ANTLRAbstractToken **)
- calloc(chunk_size+1,sizeof(ANTLRAbstractToken *)))+1;
- #endif
- if ( buffer == NULL ) {
- panic("cannot alloc token buffer");
- }
- tp = &buffer[0];
- last = tp-1;
- next = &buffer[0];
- num_markers = 0;
- end_of_buffer = &buffer[buffer_size-1];
- }
-
- ANTLRTokenBuffer::
- ~ANTLRTokenBuffer()
- {
- if ( buffer!=NULL ) free((char *)(buffer-1));
- }
-
- //#include <stdio.h>
-
- ANTLRAbstractToken *ANTLRTokenBuffer::
- getToken()
- {
- if ( tp <= last ) // is there any buffered lookahead still to be read?
- {
- return *tp++; // read buffered lookahead
- }
- // out of buffered lookahead, get some more "real" input from getANTLRToken()
- if ( next > end_of_buffer ) // buffer overflow?
- {
- // fprintf(stderr, "getToken: next > end_of_buffer (size is %d)\n", buffer_size);
- makeRoom();
- }
- *next = getANTLRToken();
- last = next;
- next++;
- tp = last;
- return *tp++;
- }
-
- void ANTLRTokenBuffer::
- rewind(int pos)
- {
- tp = &buffer[pos];
- num_markers--;
- }
-
- /*
- * This function is used to specify that the token pointers read
- * by the ANTLRTokenBuffer should be buffered up (to be reused later).
- */
- int ANTLRTokenBuffer::
- mark()
- {
- num_markers++;
- return tp - buffer;
- }
-
- /*
- * returns the token pointer n positions ahead.
- * This implies that bufferedToken(1) gets the NEXT symbol of lookahead.
- * This is used in conjunction with the ANTLRParser lookahead buffer.
- *
- * No markers are set or anything. A bunch of input is buffered--that's all.
- * The tp pointer is left alone as the lookahead has not been advanced
- * with getToken(). The next call to getToken() will find a token
- * in the buffer and won't have to call getANTLRToken().
- *
- * If this is called before a consume() is done, how_many_more_i_need is
- * set to 'n'.
- */
- ANTLRAbstractToken *ANTLRTokenBuffer::
- bufferedToken(int n)
- {
- int how_many_more_i_need = (last-tp < 0) ? n : n-(last-tp)-1;
- // Make sure that at least n tokens are available in the buffer
- // fprintf(stderr, "bufferedToken(%d)\n", n);
- for (int i=1; i<=how_many_more_i_need; i++)
- {
- if ( next > end_of_buffer ) // buffer overflow?
- {
- extendBuffer();
- }
- *next = getANTLRToken();
- last = next;
- next++;
- }
- return tp[n - 1];
- }
-
- /* If no markers are set, the none of the input needs to be saved (except
- * for the lookahead Token pointers). We save only k-1 token pointers as
- * we are guaranteed to do a getANTLRToken() right after this because otherwise
- * we wouldn't have needed to extend the buffer.
- *
- * If there are markers in the buffer, we need to save things and so
- * extendBuffer() is called.
- */
- void ANTLRTokenBuffer::
- makeRoom()
- {
- // fprintf(stderr, "in makeRoom.................\n");
- if ( num_markers == 0 )
- {
- // fprintf(stderr, "num_markers==0; moving lookahead and resetting next\n");
- // fprintf(stderr, "before: tp=%d, last=%d, next=%d\n", tp-buffer, last-buffer, next-buffer);
- // reset the buffer to initial conditions, but move k-1 lookahead symbols
- // to the beginning of buffer and put new input symbol at k
- ANTLRAbstractToken **p = buffer, **q = end_of_buffer-(k-1);
- // fprintf(stderr, "lookahead buffer = [");
- for (int i=1; i<=k; i++)
- {
- *p++ = *q++;
- // fprintf(stderr, " '%s'", ((ANTLRCommonToken *)buffer[i-1])->getText());
- }
- // fprintf(stderr, "]\n");
- next = &buffer[k];
- tp = &buffer[k]; // tp points to what will be filled in next
- last = tp-1;
- // fprintf(stderr, "after: tp=%d, last=%d, next=%d\n", tp-buffer, last-buffer, next-buffer);
- }
- else {
- extendBuffer();
- }
- }
-
- /* This function extends 'buffer' by chunk_size and returns with all
- * pointers at the same relative positions in the buffer (the buffer base
- * address could have changed in realloc()) except that 'next' comes
- * back set to where the next token should be stored. All other pointers
- * are untouched.
- */
- void
- ANTLRTokenBuffer::
- extendBuffer()
- {
- int save_last = last-buffer, save_tp = tp-buffer, save_next = next-buffer;
- // fprintf(stderr, "extending physical buffer\n");
- buffer_size += chunk_size;
- #ifdef OLD
- buffer = (ANTLRAbstractToken **)
- realloc((char *)buffer, buffer_size * sizeof(ANTLRAbstractToken *));
- #else
- buffer = ((ANTLRAbstractToken **)
- realloc((char *)(buffer-1), (buffer_size+1)*sizeof(ANTLRAbstractToken *)))+1;
- #endif
- if ( buffer == NULL ) {
- panic("cannot alloc token buffer");
- }
- tp = buffer + save_tp; // put the pointers back to same relative position
- last = buffer + save_last;
- next = buffer + save_next;
- end_of_buffer = &buffer[buffer_size-1];
- }
-